package com.alipay.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayRequest;
import com.alipay.api.AlipayResponse;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.config.AlipayConfig;
import com.alipay.constant.ProductCode;
import com.alipay.model.request.ScanPayRequest;
import com.alipay.model.response.MiniAppPayResponse;
import com.alipay.model.response.ScanPayResponse;
import com.alipay.service.AbstractPayService;

/**
 * @author lwj
 * @create 2025-10-15-16:13
 */
public class ScanPayServiceImpl extends AbstractPayService<ScanPayRequest, ScanPayResponse> {
    public ScanPayServiceImpl(AlipayClient alipayClient, AlipayConfig alipayConfig) {
        super(alipayClient, alipayConfig);
    }

    @Override
    protected AlipayRequest createAlipayRequest() {
        return new AlipayTradePrecreateRequest();
    }

    @Override
    protected void setRequestParams(AlipayRequest alipayRequest, JSONObject bizContent) {
        // 强转为具体请求类型
        AlipayTradePrecreateRequest precreateRequest = (AlipayTradePrecreateRequest) alipayRequest;
        // 将 JSON 类型转换为 Model 型
        AlipayTradePrecreateModel model = bizContent.toJavaObject(AlipayTradePrecreateModel.class);
        // 设置请求参数
        precreateRequest.setBizModel(model);
    }

    @Override
    protected void fillBizContent(JSONObject bizContent, ScanPayRequest request) {
        // 添加二维码超时时间
        bizContent.put("qr_code_timeout_express", request.getQrCodeTimeoutExpress());
    }

    @Override
    protected AlipayResponse executeAlipayRequest(AlipayRequest alipayRequest) throws AlipayApiException {
        // 执行请求
        return alipayClient.execute((AlipayTradePrecreateRequest) alipayRequest);
    }

    @Override
    protected ScanPayResponse handleAlipayResponse(AlipayResponse alipayResponse, ScanPayRequest request) {
        ScanPayResponse response = new ScanPayResponse();
        AlipayTradePrecreateResponse precreateResponse = (AlipayTradePrecreateResponse) alipayResponse;

        // 填充自定义响应对象数据
        return populateResponse(response,precreateResponse);
    }

    @Override
    protected ScanPayResponse createEmptyResponse() {
        return new ScanPayResponse();
    }

    @Override
    protected String getProductCode() {
        return ProductCode.SCAN_PAY;
    }

    @Override
    protected void populateSpecificFields(ScanPayResponse response, AlipayResponse alipayResponse) {
        // 强转为扫码支付响应
        AlipayTradePrecreateResponse precreateResponse = (AlipayTradePrecreateResponse) alipayResponse;
        // 设置二维码地址
        response.setQrCode(precreateResponse.getQrCode());
    }
}
